package com.joysuch.wwyt.attendance.util;

import com.joysuch.wwyt.attendance.bean.AttendanceDayBean;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
import org.apache.poi.xssf.usermodel.XSSFSheet;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

/**
 * 获取一个月的所有排班日期
 *
 * @Author ZhaoQiuWei
 * @Date 2022/3/3 17:08
 */
public class AttendanceDayUtils {

    public static List<AttendanceDayBean> getDayByMonth(int yearParam, int monthParam) {
        List<AttendanceDayBean> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.set(yearParam, monthParam - 1, 1);
        int year = calendar.get(Calendar.YEAR);//年份
        int month = calendar.get(Calendar.MONTH) + 1;//月份
        int day = calendar.getActualMaximum(Calendar.DATE);
        for (int i = 1; i <= day; i++) {
            list.add(new AttendanceDayBean(year, month, i, count(year, month, i)));
        }
        return list;
    }

    public static boolean getDateCheckFlag(int paramYear, int paramMonth) {
        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        int month = now.getMonthValue();
        boolean userFlag = true;
        if (paramYear < year) {
            userFlag = false;
        } else {
            if (paramYear == year && paramMonth < month) {
                userFlag = false;
            }
        }
        return userFlag;
    }

    public static String count(int y, int m, int d) {
        if (m == 1) {
            m = 13;
            y--;
        }
        if (m == 2) {
            m = 14;
            y--;
        }
        int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;
        String weekstr = "周";
        switch (week) {
            case 0:
                weekstr += "一";
                break;
            case 1:
                weekstr += "二";
                break;
            case 2:
                weekstr += "三";
                break;
            case 3:
                weekstr += "四";
                break;
            case 4:
                weekstr += "五";
                break;
            case 5:
                weekstr += "六";
                break;
            case 6:
                weekstr += "日";
                break;
        }
        return weekstr;
    }
//2 500，12， 12
    public static void createCells(XSSFSheet spreadsheet, String[] strs, int firstRow, int lastRow, int firstCol, int lastCol) {
        // firstRow第几行开始   lastRow第几行结束, int firstCol第几列开始, int lastCol第几列结束
        CellRangeAddressList regions = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
        DataValidationHelper dataValidationHelper = spreadsheet.getDataValidationHelper();
        DataValidationConstraint createExplicitListConstraint = dataValidationHelper.createExplicitListConstraint(strs);
        DataValidation createValidation = dataValidationHelper.createValidation(createExplicitListConstraint, regions);
        //处理Excel兼容性问题
        if (createValidation instanceof XSSFDataValidation) {
            createValidation.setSuppressDropDownArrow(true);
            createValidation.setShowErrorBox(true);
        } else {
            createValidation.setSuppressDropDownArrow(false);
        }
        spreadsheet.addValidationData(createValidation);
    }


    public static String getCellStringValue(XSSFCell cell) {
        Object obj = getCellValue(cell, true);
        return obj == null ? null : obj.toString().trim();
    }

    private static Object getCellValue(XSSFCell cell, boolean isText) {
        CellType cellType = cell.getCellType();
        DecimalFormat df = new DecimalFormat("0");


        Object value = null;
        switch (cellType) {
            case STRING: {
                value = cell.getStringCellValue();
                break;
            }
            case NUMERIC: {
                value = cell.getNumericCellValue();
                if (isText) {
                    BigDecimal decimal = new BigDecimal(value.toString());
                    int scale = decimal.scale();
                    if (scale < 1) {
                        //小于1代表科学计数或者整数
                        value = df.format(decimal);
                    } else {
                        value = String.valueOf(decimal);
                    }
                    return String.valueOf(value);
                }
                break;
            }
            case FORMULA: {
                value = cell.getNumericCellValue();
                if (isText) {
                    return value.toString();
                }
                break;
            }
        }
        return value;
    }
}
